/**
 * Actualisation de la valeur de la cotation en faux temps réel (30 sec)
 */

package com.miage.itrading.metier;

import java.awt.Color;
import java.sql.SQLException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.miage.itrading.dao.CotationDAO;
import com.miage.itrading.entites.Cotation;
import com.miage.itrading.entites.Intraday;
import com.miage.itrading.utils.Util;


public class CoursTempsReelDaemon extends Thread {
	
	JButton 	componentButton;
	JLabel		componentLabel;
	
	JTable		table;
	int			row;
	int			columnCours;
	int			columnVariation;
	
	Cotation 	cotation;
	long 		intervalleActualisationSec = 30;
	
	
	public CoursTempsReelDaemon(Cotation cotation, JButton component) {
		this.cotation  = cotation;
		this.componentButton = component;
		
		setIntervalleActualisationSec(intervalleActualisationSec);
	}
	
	public CoursTempsReelDaemon(Cotation cotation, JLabel component) {
		this.cotation  		= cotation;
		this.componentLabel	= component;
		
		setIntervalleActualisationSec(intervalleActualisationSec);
	}
	
	
	public CoursTempsReelDaemon(Cotation c, long intervalleActualisationSec) {
		this.cotation = c;
		
		setIntervalleActualisationSec(intervalleActualisationSec);
	}


	public CoursTempsReelDaemon(Cotation cotation, JTable table, int row, int columnNom, int columnVariation) {
		this.cotation			= cotation;
		this.table				= table;
		this.row				= row;
		this.columnCours		= columnNom;
		this.columnVariation	= columnVariation;
		
		setIntervalleActualisationSec(intervalleActualisationSec);
	}

	public long getIntervalleActualisationSec() {
		return intervalleActualisationSec;
	}
	
	
	public void setIntervalleActualisationSec(long i) {
		this.intervalleActualisationSec = (Math.abs(i) > 0 ? Math.abs(i) : 1);
	}
	

	public void run() {
		String nom = cotation.getNom();
		CotationDAO c = new CotationDAO(cotation);
		
		while (true) {
			try {
				/**
				 * Met le thread en pause si la bourse est fermée
				 */
//				if (Util.tempsReelPause > 0) {
//					Thread.sleep(Util.tempsReelPause);	
//				}
				
				Intraday cotationCourante = c.getCotationCourante(cotation.getIdCotation()); 
				cotation.mergeIntraday(cotationCourante);
				
				if (componentButton != null) {
					componentButton.setText(nom + " (" + Util.variationCoursString(cotation) + ")");
					componentButton.setForeground(Util.styleVariationCours(cotation));		
					
				}
				else if (componentLabel != null) {
					componentLabel.setText(nom + " (" + Util.variationCoursString(cotation) + ")");
					componentLabel.setForeground(Util.styleVariationCours(cotation));
					
				}
				else if (table != null) {
					if (cotationCourante.getCotation() == 0) {
						table.setValueAt(cotation.getValeurClotureVeille() + " €", row, columnCours);
					}
					else {
						table.setValueAt(cotationCourante.getCotation() + " €", row, columnCours);	
					}
					
					table.setValueAt(Util.variationCoursString(cotation), row, columnVariation);
					
					Color color = Util.styleVariationCours(cotation);
					TableCellRenderer tcr;
					tcr = table.getCellRenderer(row, columnCours);
					tcr.getTableCellRendererComponent(table, null, true, true, row, columnCours).setForeground(color);
					
					tcr = table.getCellRenderer(row, columnVariation);
					tcr.getTableCellRendererComponent(table, null, true, true, row, columnVariation).setForeground(color);
				}
				
				Thread.sleep(intervalleActualisationSec * 1000);
				
			} catch (SQLException e) {
				e.printStackTrace();
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
